﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Share.Log;
using Microsoft.VisualBasic.Activities;
using System;
using System.Activities;
using System.Activities.Debugger.Symbol;
using System.Activities.Presentation;
using System.Activities.Validation;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Xaml;

namespace DWF.Studio.RemoteCtrls.SnippetExplorer
{
    public class SnippetTemplateFactory : IActivityTemplateFactory
    {
        public static string SnippetFullName
        {
            set;
            get;
        }
        /// <summary>
        /// 片段缺少依赖活动
        /// </summary>
        public static Action<EventArgs> CheckSnippetActivity = null;
        public Activity Create(DependencyObject target)
        {
            Activity activitys = null;
            try
            {
                activitys = ActivityXamlServices.Load(SnippetFullName);
                AttachableMemberIdentifier debugSymbol = new AttachableMemberIdentifier(typeof(DebugSymbol), "Symbol");
                AttachablePropertyServices.RemoveProperty(activitys, debugSymbol);

                DynamicActivity dynamicActivity = activitys as DynamicActivity;
                if (dynamicActivity != null)
                {
                    ActivityBuilder activityBuilder = new ActivityBuilder();
                    activityBuilder.Implementation = dynamicActivity.Implementation();
                    activityBuilder.Name = dynamicActivity.Name;

                    if (activityBuilder.Implementation != null)
                    {
                        AttachablePropertyServices.RemoveProperty(activityBuilder.Implementation, debugSymbol);
                    }

                    foreach (Attribute attribute in dynamicActivity.Attributes)
                    {
                        activityBuilder.Attributes.Add(attribute);
                    }

                    foreach (Constraint constraint in dynamicActivity.Constraints)
                    {
                        activityBuilder.Constraints.Add(constraint);
                    }

                    foreach (DynamicActivityProperty property in dynamicActivity.Properties)
                    {
                        DynamicActivityProperty activityProperty = new DynamicActivityProperty
                        {
                            Name = property.Name,
                            Type = property.Type,
                            Value = null
                        };

                        foreach (Attribute attribute in property.Attributes)
                        {
                            activityProperty.Attributes.Add(attribute);
                        }

                        activityBuilder.Properties.Add(activityProperty);
                    }

                    VisualBasic.SetSettings(activityBuilder, VisualBasic.GetSettings(dynamicActivity));

                    activitys = activityBuilder.Implementation;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return null;
            }

            return activitys;
        }
    }
}
